package com.zr.spring.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Types;
import java.util.List;

public class PersonServiceImpl implements PersonService {

    private JdbcTemplate jdbcTemplate;

    //设置数据源

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void save(Person person) {
        this.jdbcTemplate.update("insert into person(user_name,age) value (?,?)", new Object[]{person.getUserName(), person.getAge()},
                new int[]{Types.VARCHAR, Types.INTEGER});
    }

    @Override
    public List<Person> getPerson() {
        List<Person> list = this.jdbcTemplate.query("select * from person", new PersonRowMapper());
        return list;
    }
}
